// pages/member/earningList/earningList.js
var app = getApp();
Page({

  /**
	 * 页面的初始数据
	 */
  data: {
    language: '',
    plugin_type: '',
    plugin_text:'',
    isLoadMore: true,
    page: 1,
    total_page: 0,
    earningList: [],
    //广告
    adv_thumb: '',
    adv_url: '',
    income: 0,
    reward_type:'',

    //经纬度
    lat:"",
    lng:"",
  },

  /**
	 * 生命周期函数--监听页面加载
	 */
  onLoad: function(options) {
    if (options.plugin_type) {
      this.setData({
        plugin_type: options.plugin_type,
        plugin_text: options.plugin_text,
      });
    }
    this.loaction();
    this._getData();
  },
  loaction(){
    let myLoaction = wx.getStorageSync('address-point')
    if(myLoaction && myLoaction.point){
      this.setData({lat:myLoaction.point.lat,lng:myLoaction.point.lng})
    }else{
      this.setData({lat:'',lng:''})
    }
  },

  /**
	 * 生命周期函数--监听页面初次渲染完成
	 */
  onReady: function() {
    let language = wx.getStorageSync('langIndex');
    this.setData({ 'language': language.en});
  },

  /**
	 * 生命周期函数--监听页面显示
	 */
  onShow: function() {

  },

  /**
	 * 生命周期函数--监听页面隐藏
	 */
  onHide: function() {

  },

  /**
	 * 生命周期函数--监听页面卸载
	 */
  onUnload: function() {

  },

  /**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
  onPullDownRefresh: function() {

  },

  /**
	 * 页面上拉触底事件的处理函数
	 */
  onReachBottom: function() {
    if (this.data.isLoadMore) {
      this._getMoreData();
    } else {
      console.log('没有更多数据');
    }
  },

  /**
	 * 用户点击右上角分享
	 */
  onShareAppMessage: function() {

  },
  //获取数据
  _getData() {
    let that = this;
    that.setData({
      isLoadMore: false,
      page: 1
    });
    let urlStr = app.getNetAddresss('finance.plugin-settle.get-not-settle-list');
    urlStr += '&plugin_type=' + that.data.plugin_type ; 
    app._getNetWork({
      url: urlStr,
      success: function(resdata) {
        var res = resdata.data;
        if (res.result == 1) {
          that.setData({
            isLoadMore: true,
            total_page: res.data.last_page
          });
          if (!that.data.total_page) {
            that.setData({
              total_page: 0
            });
          }
          that.setData({
            earningList: res.data.data
          });
        } else {
          that.setData({
            earningList: res.data
          });
        }
      },
      fail: function(res) {
        console.log(res.msg);
      }
    });
  },
  //获取更多数据
  _getMoreData() {
    let that = this;
    that.setData({
      isLoadMore: false
    });
    if (this.data.page >= this.data.total_page) {
      return;
    } else {
      that.setData({
        page: that.data.page + 1
      });
      let urlStr = app.getNetAddresss('finance.plugin-settle.get-not-settle-list');
      urlStr += '&plugin_type=' + that.data.plugin_type;
      urlStr += '&page=' + that.data.page;
      app._getNetWork({
        url: urlStr,
        success: function(resdata) {
          var res = resdata.data;
          that.setData({
            isLoadMore: true
          });
          if (res.result == 1) {
            let myData = res.data.data;
            that.setData({
              earningList: that.data.earningList.concat(myData)
            });
          } else {
            that.setData({
              page: that.data.page - 1,
              isLoadMore: false
            });
            return;
          }
        },
        fail: function(res) {
          console.log(res.msg);
        }
      });
    }
  },
  getAdvertising(e) {
    let that = this;
    let id = e.currentTarget.dataset.id;
    that.setData({
      adv_thumb: '',
      adv_url: '',
      income: 0
    });
    let urlStr = app.getNetAddresss('finance.plugin-settle.income-receive');
    urlStr += '&plugin_type=' + that.data.plugin_type;
    urlStr += '&id=' + id 
    urlStr += '&lng=' + this.data.lng
    urlStr += '&lat=' + this.data.lat;
    app._getNetWork({
      url: urlStr,
      success: function(resdata) {
        var res = resdata.data;
        if (res.result == 1) {
          that.setData({
            adv_thumb: res.data.adv_thumb,
            adv_url: res.data.adv_url,
            income: res.data.income_data.amount,
            show1: true
          });
          if (res.data.income_data && res.data.income_data.reward_type) {
            that.setData({
              reward_type: res.data.income_data.reward_type
            });
          }
          that._getData();
        } else {
          wx.showToast({
            icon: 'none',
            title: res.msg,
            duration: 1500
          });
        }
      },
      fail: function(res) {
        console.log(res.msg);
      }
    });
  },
  toAdvertising() {
    //adv_url
    this.setData({
      show1: false
    });
  },
});
